<template>
    <div class="edu-page-warp">
        <condition-card
            :showResetBtn="true"
            @search="searchList"
            @reset="reset"
            label-width="110px"
        >
            <el-row>
                <el-col :span="8">
                    <el-form-item label="入学年级">
                        <!-- <GradeSelect v-model="queryParams.grade" :showAll="false"></GradeSelect> -->
                        <el-date-picker
                            style="width:100%"
                            v-model="queryParams.grade"
                            align="right"
                            type="year"
                            value-format="yyyy"
                            placeholder="请选择"
                            :clearable="true">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item
                        label="当前年级"
                        :error="(!queryParams.currentGrade && !!queryParams.faculty) ? '请选择当前年级':''"
                    >
                        <!-- <GradeSelect v-model="queryParams.currentGrade" :showAll="false"></GradeSelect> -->
                        <el-date-picker
                            style="width:100%"
                            v-model="queryParams.currentGrade"
                            align="right"
                            type="year"
                            value-format="yyyy"
                            placeholder="请选择"
                            :clearable="true">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="包括国际学生">
                        <YesNoSelect v-model="queryParams.isInternational"></YesNoSelect>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="学院" v-if="tutorType ==2">
                        <DictSelect
                            v-model="queryParams.faculty"
                            :data="options.faculty"
                            filterable
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="学堂" v-if="tutorType ==1">
                        <DictSelect
                            v-model="queryParams.schoolId"
                            :data="options.school"
                            filterable
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="专业" v-if="tutorType ==2">
                        <!-- <MajorSelect :faculty="queryParams.faculty" v-model="queryParams.professionCode"></MajorSelect> -->
                        <MyMajor
                            :faculty="queryParams.faculty"
                            :grade="queryParams.currentGrade"
                            v-model="queryParams.professionCode"
                        ></MyMajor>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="所属类" v-if="tutorType ==1">
                        <SchoolMajorSelect
                            :schoolId="queryParams.schoolId"
                            v-model="queryParams.professionCode"
                        ></SchoolMajorSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="状态">
                        <DictSelect v-model="queryParams.chooseStatus" :data="options.status"></DictSelect>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="学籍状态">
                        <DictSelect
                            v-model="queryParams.registrationStatus"
                            :data="options.studentStatus"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="tutorType ==1">
                    <el-form-item label="申请开始时间">
                        <el-date-picker
                            type="datetime"
                            :placeholder="$t('message.startDate')"                          
                            value-format="yyyy-MM-dd HH:mm:ss"
                            v-model="queryParams.startTime"
                            style="width:100%;hegiht:100:"
                            :clearable="true"
                        ></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="tutorType ==1">
                    <el-form-item label="申请结束时间">
                        <el-date-picker
                            type="datetime"
                            :placeholder="$t('message.endDate')"                            
                            value-format="yyyy-MM-dd HH:mm:ss"
                            v-model="queryParams.endTime"
                            style="width:100%;hegiht:100:"
                            :clearable="true"
                        ></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
        </condition-card>
        <el-card shadow="never">
            <div class="edu-page-title">{{topTitle}}</div>
            <div class="edu-opertion-bar">
                <div class="float-left">
                    <el-col :span="8">
                        <DictSelect
                            v-model="queryParams.selectType"
                            :data="options.selectType"
                            :clearable="false"
                        ></DictSelect>
                    </el-col>
                    <KeywordInput
                        v-model="queryParams.keyword"
                        @enter="searchList"
                        :placeholder="queryParams.selectType=='1'?'按学号或姓名搜索':'按工号或姓名搜索'"
                    ></KeywordInput>
                </div>
                <div class="float-right">
                    <el-dropdown @command="importStuTutor">
                        <el-button v-premit="import_StuTutor">
                            导入
                            <i class="el-icon-arrow-down el-icon--right"></i>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item command="in">导入</el-dropdown-item>
                            <el-dropdown-item command="out">下载模板</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                    <el-button
                        @click="exportStuTutor"
                        v-premit="export_StuTutor"
                    >{{$t('message.export')}}</el-button>
                    <el-button @click="autoAllocation" type="primary" v-premit="auto_StuTutor">自动分配</el-button>
                    <el-button @click="pointTutors" type="primary" v-premit="tutor_point">导师指定</el-button>
                    <el-button @click="release" type="primary" v-premit="tutor_release">发布</el-button>
                </div>
            </div>
            <edu-table
                url="/api/welcomeservice/tutorStudent/list"
                ref="mainTable"
                :order="false"
                :selection="true"
                @select="handleCheckbox"
            >
                <el-table-column label="学号" prop="studentId"/>
                <el-table-column label="姓名" prop="studentName" show-overflow-tooltip/>
                <el-table-column label="性别" prop="sexI18n"/>
                <el-table-column
                    label="学院"
                    prop="facultyI18n"
                    v-if="tutorType  == 2"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="专业"
                    prop="professionCodeI18n"
                    v-if="tutorType  == 2"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="学堂"
                    prop="school"
                    v-if="tutorType  == 1"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="所属类"
                    prop="professionCodeI18n"
                    v-if="tutorType == 1"
                    show-overflow-tooltip
                />
                <el-table-column label="当前年级" prop="currentGrade"/>
                <el-table-column label="联系方式" prop="phone" show-overflow-tooltip/>
                <el-table-column label="状态" prop="chooseStatus">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.chooseStatus == 1" type="success">已选择</el-tag>
                        <el-tag v-else>未选择</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="导师工号" prop="teacherCode"/>
                <el-table-column label="导师姓名" prop="teacherName"/>
                <el-table-column label="导师所属学院" prop="tutorFacultyI18n" show-overflow-tooltip>
                    <template slot="header" slot-scope="scope">
                        <el-tooltip class="item" effect="light" content="导师所属学院" placement="top">
                            <span>导师所属学院</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="申请时间" prop="applyTime" show-overflow-tooltip/>
                <el-table-column label="是否发布" prop="applyStatus">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.applyStatus == 1" type="success">已发布</el-tag>
                        <el-tag v-else>未发布</el-tag>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('message.operating')" fixed="right" width="160">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="pointTutor(scope.row)"
                            size="mini"
                            v-premit="'student_point'"
                        >导师指定</el-button>
                        <el-button v-if="scope.row.id"
                            type="text"
                            @click="delTutorList(scope.row)"
                            size="mini"
                            v-premit="'stuDel_point'"
                        >清除导师关系</el-button>
                    </template>
                </el-table-column>
            </edu-table>
        </el-card>
        <!-- 指定学生 -->
        <edu-dialog
            :title="message"
            width="1000px"
            ref="tutorDialog"
            :auto-height="true"
            @close="resetDialog"
        >
            <div class="edu-page-warp">
                <condition-card
                    :showResetBtn="true"
                    @reset="resetTutor"
                    @search="searchTutorList"
                    label-width="100px"
                >
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="学院" v-if="tutorType ==2">
                                <DictSelect
                                    v-model="queryTutor.faculty"
                                    :data="options.faculty"
                                    filterable
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="学堂" v-if="tutorType ==1">
                                <DictSelect
                                    v-model="queryTutor.schoolId"
                                    :data="options.dialogSchool"
                                    filterable
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="专业" v-if="tutorType ==2 ">
                                <!-- <MajorSelect :faculty="queryTutor.faculty" v-model="queryTutor.major"></MajorSelect> -->
                                <MyMajor
                                    :faculty="queryTutor.faculty"
                                    :grade="queryTutor.currentGrade"
                                    v-model="queryTutor.major"
                                    :clearOnGrade="false"
                                ></MyMajor>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="所属类" v-if="tutorType ==1">
                                <SchoolMajorSelect
                                    :schoolId="queryTutor.schoolId"
                                    v-model="queryTutor.major"
                                ></SchoolMajorSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="职称">
                                <DictSelect
                                    v-model="queryTutor.title"
                                    :data="options.title"
                                    filterable
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </condition-card>
                <el-card shadow="never">
                    <div class="edu-page-title">{{showTitle}}</div>
                    <div class="edu-opertion-bar">
                        <div class="float-left">
                            <KeywordInput
                                v-model="queryTutor.condition"
                                @enter="searchTutorList"
                                placeholder="请输入工号或姓名搜索"
                            ></KeywordInput>
                        </div>
                    </div>
                    <edu-table
                        url="/api/welcomeservice/tutorList/list"
                        ref="tutorTable"
                        :order="false"
                        :selection="false"
                    >
                        <el-table-column label="工号" prop="teacherCode"/>
                        <el-table-column label="姓名" prop="teacherName"/>
                        <el-table-column label="性别" prop="sexI18n"/>
                        <el-table-column label="职称" prop="titleI18n"/>
                        <el-table-column label="计划人数" prop="planNum"/>
                        <el-table-column label="实际人数" prop="actNum"/>
                        <el-table-column :label="$t('message.operating')" fixed="right">
                            <template slot-scope="scope">
                                <el-button type="text" @click="save(scope.row)" size="mini">指定</el-button>
                            </template>
                        </el-table-column>
                    </edu-table>
                </el-card>
            </div>
            <span slot="buttons">
                <el-button @click="close">{{$t('message.cancel')}}</el-button>
            </span>
        </edu-dialog>

        <!-- 批量导师指定 -->
        <edu-dialog
            :title="pointTutorTitle"
            width="500px"
            ref="pointTutorsDialog"
            :auto-height="true"
            @close="resetPointTutorsDialog"
            @confirm="submitFormTutors"
        >
            <div class="edu-page-warp">
                <el-form
                    :model="queryTutors"
                    ref="queryTutors"
                    label-width="100px"
                    label-suffix=":"
                >
                    <el-col :span="20">
                        <el-form-item
                            label="导师工号"
                            prop="condition.condition"
                            :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]"
                        >
                            <TutorSelect
                                v-model="queryTutors.condition.condition"
                                :condition="queryTutorsParam"
                                value="id"
                                :clearable="false"
                                :major="major"
                                :tutorGrade="tutorGrade"
                                :type="tutorType"
                            ></TutorSelect>
                        </el-form-item>
                    </el-col>
                </el-form>
            </div>
        </edu-dialog>

        <!-- 导入导师学生关系 -->
        <edu-dialog
            :title="$t('message.import')"
            size="small"
            ref="uploadDialog"
            @close="clearFiles"
            @confirm="uploadFile"
        >
            <el-form :model="createForm" ref="createForm" label-width="85px" v-loading="loading1">
                <el-row>
                    <el-form-item>
                        <el-upload
                            ref="upload"
                            :data="{}"
                            :on-success="uploadDone"
                            :auto-upload="false"
                            :multiple="false"
                            :on-change="(file, fileList)=>{this.fileList = fileList}"
                            :limit="1"
                            :accept="'application/vnd.ms-excel'"
                            :action="importUrl"
                        >
                            <el-button
                                slot="trigger"
                                size="small"
                                type="primary"
                            >{{$t('label.selectFile')}}</el-button>
                            <div
                                slot="tip"
                                class="el-upload__tip"
                            >{{$t('courseTake.uploadXLSFileOnly')}}</div>
                        </el-upload>
                    </el-form-item>
                </el-row>
            </el-form>
        </edu-dialog>
    </div>
</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import BPIs from "common/src/api/welcomeService/schoolRelation";
import SchoolMajorSelect from "./SchoolMajorSelect";
import TutorSelect from "./TutorSelect";
import MyMajor from "./MyMajor";
import APIs from "common/src/api/welcomeService/tutorList";
import UTILS from "common/src/utils/utils";
import CPIs from "common/src/api/welcomeService/tutorExportImport";

// 本科生导师库管理
const defaultForm = {
    grade: null,
    currentGrade: null,
    faculty: "",
    professionCode: "",
    schoolId: "",
    chooseStatus: "",
    selectType: "1",
    stuCondition: "",
    tutorCondition: "",
    keyword: "",
    registrationStatus: "",
    type: ""
};
const tutorForm = {
    condition: "",
    schoolId: "",
    faculty: "",
    major: "",
    title: "",
    type: "",
    status:1
};
export default {
    name: "TutorListManage",
    props: ["tutorType"],
    computed: {},
    components: {
        SchoolMajorSelect,
        TutorSelect,
        MyMajor
    },
    mounted() {
        this.getOptions();
        if(this.tutorType == 1){
            this.queryParams.currentGrade=String(new Date().getFullYear());
            defaultForm.currentGrade=this.queryParams.currentGrade;
            this.handleCurrentGradeChange(this.queryParams.currentGrade)
        }
    },
    watch: {
        "queryParams.currentGrade": {
            handler: function(newVal, oldVal) {
                this.handleCurrentGradeChange(newVal);
            }
        }
    },
    data() {
        return {
            dialogTitle: "",
            topTitle: "",
            showTitle: "",
            message: "",
            loading: false,
            loading1: false,
            pointTutorTitle: "",
            importUrl: "",
            tutorList: [],
            stuTutor: null,
            fileList: [],
            selectionList: [],
            major: "",
            tutorGrade: null,
            options: {
                faculty: [],
                major: [],
                title: [],
                status: [
                    { label: "未选择", value: "0" },
                    { label: "已选择", value: "1" }
                ],
                selectType: [
                    { label: "按学生搜索", value: "1" },
                    { label: "按教师搜索", value: "2" }
                ],
                studentStatus: [],
                school: [],
                dialogSchool: []
            },
            queryTutor: UTILS.clone(tutorForm),
            querySchool: {
                pageNum_: 0,
                pageSize_: 0,
                condition: {}
            },
            queryParams: UTILS.clone(defaultForm),
            queryTutors: {
                pageSize_: 20,
                pageNum_: 1,
                condition: {
                    condition: ""
                }
            },
            queryTutorsParam: {
                pageSize_: 20,
                pageNum_: 1,
                condition: {
                    type: this.tutorType,
                    condition: "",
                    major: "",
                    tutorGrade: null
                }
            }
        };
    },
    methods: {
        getOptions() {
            //筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_YX", "G_ZY", "G_XJZT", "profession"]
            };
            queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        faculty: "X_YX",
                        major: "G_ZY",
                        studentStatus: "G_XJZT",
                        title: "profession"
                    },
                    res.data,
                    this.options
                );
            });
            this.topTitle =
                this.tutorType == 1 ? "新生导师管理" : "学院导师管理";
            this.message =
                this.tutorType == 1 ? "新生导师学生指定" : "学院导师学生指定";
            this.searchList();
        },
        exportStuTutor() {
            let loading = this.$loading({
                lock: true,
                text: "Loading",
                spinner: "el-icon-loading",
                background: "rgba(255, 255, 255, .48)"
            });
            this.queryParams.stuCondition = "";
            this.queryParams.tutorCondition = "";
            if (this.queryParams.selectType == 1) {
                //学生
                this.queryParams.stuCondition = this.queryParams.keyword;
            } else {
                this.queryParams.tutorCondition = this.queryParams.keyword;
            }
            this.queryParams.type = this.tutorType;
            let data = _.cloneDeep(this.queryParams);
            data.studentIds = this.$refs.mainTable
                .getCheckData()
                .map(c => c.studentId);
            CPIs.exportStuTutor(data).then(res => {
                if (res.code == 200) {
                    this.delayRequest(res.data.key, loading);
                } else {
                    loading.close();
                }
            });
        },
        async delayRequest(key, loading) {
            await new Promise(resolve => setTimeout(resolve, 3000));
            CPIs.getReportStatus(key)
                .then(res => {
                    if (res.code == 200 && res.data.status) {
                        let path = CPIs.exportLogUrl + res.data.path;
                        //window.open(path);
                        window.location.href = path;
                        loading.close();
                    } else {
                        this.delayRequest(key, loading);
                    }
                })
                .catch(ex => {
                    this.delayRequest(key, loading);
                });
        },
        importStuTutor(type) {
            this.importUrl = "";
            if (type == "in") {
                //导入
                this.importUrl =
                    "/api/welcomeservice/tutorStudent/import/" + this.tutorType;
                this.$refs.uploadDialog.open();
            } else if (type == "out") {
                //下载导入模板
                window.location.href = CPIs.tutorManageTemplate(this.tutorType);
            }
        },
        clearFiles() {
            this.$refs.upload.clearFiles();
            this.fileList = [];
        },
        uploadFile() {
            if (_.isEmpty(this.fileList)) {
                this.$message({
                    type: "info",
                    message: this.$t("message.selectFilePlease")
                });
                return;
            }
            this.$refs.upload.submit();
        },
        uploadDone(res, file, fileList) {
            if (res.code == 200) {
                this.loading1 = true;
                this.delayImportRequest(res.data.key);
            } else {
                this.$message({
                    type: "error",
                    message: this.$t("common.tips.dataFormatError")
                });
                this.clearFiles();
            }
        },
        async delayImportRequest(key) {
            await new Promise(resolve => setTimeout(resolve, 3000));
            CPIs.getImportStatus(key)
                .then(res => {
                    if (res.code == 200 && res.data.status) {
                        this.loading1 = false;
                        this.clearFiles();
                        if (res.data.success) {
                            this.$message({
                                type: "success",
                                message: this.$t("common.tips.uploadSuccess")
                            });
                            this.$refs.uploadDialog.close();
                            this.searchList();
                        } else {
                            this.$message({
                                type: "error",
                                message: res.data.msg
                            });
                        }
                    } else {
                        this.delayImportRequest(key);
                    }
                })
                .catch(ex => {
                    this.delayImportRequest(key);
                });
        },
        reset() {
            for (const key in this.queryParams) {
                if (key != "selectType") this.queryParams[key] = "";
            }
            this.queryParams.currentGrade=defaultForm.currentGrade;
            this.searchList();
        },
        searchList() {
            this.queryParams.type = this.tutorType;
            this.queryParams.stuCondition = "";
            this.queryParams.tutorCondition = "";
            if (this.queryParams.selectType == 1) {
                //学生
                this.queryParams.stuCondition = this.queryParams.keyword;
            } else {
                this.queryParams.tutorCondition = this.queryParams.keyword;
            }
            this.$refs.mainTable.search(this.queryParams);
        },
        pointTutors() {
            this.pointTutorTitle = "导师指定";
            let profession = [];
            let grades = [];
            this.major = "";
            this.type = "";
            this.queryTutorsParam.condition.type = "";
            this.queryTutorsParam.condition.major = "";
            this.queryTutorsParam.condition.condition = "";
            this.queryTutors.condition.condition = "";
            this.selectionList = this.$refs.mainTable.getCheckData();
            if (UTILS.isEmpty(this.selectionList)) {
                if (this.tutorType == 1) {
                    this.$message({
                        message: "请选择相同当前年级和相同所属类的学生",
                        type: "warning"
                    });
                } else {
                    this.$message({
                        message: "请选择相同专业的学生",
                        type: "warning"
                    });
                }

                return;
            } else {
                for (const stu of this.selectionList) {
                    stu.type = this.tutorType;
                    if (this.tutorType == 1) {
                        if (stu.professionCode) {
                            if (UTILS.isEmpty(profession)) {
                                profession.push(stu.professionCode);
                            } else {
                                if (!profession.includes(stu.professionCode)) {
                                    this.$message({
                                        message:
                                            "请选择相同当前年级和相同所属类的学生",
                                        type: "warning"
                                    });
                                    return;
                                }
                            }
                        }
                        if (stu.currentGrade) {
                            if (UTILS.isEmpty(grades)) {
                                grades.push(stu.currentGrade);
                            } else {
                                if (!grades.includes(stu.currentGrade)) {
                                    this.$message({
                                        message:
                                            "请选择相同当前年级和相同所属类的学生",
                                        type: "warning"
                                    });
                                    return;
                                }
                            }
                        }
                    } else {
                        if (stu.professionCode) {
                            if (UTILS.isEmpty(profession)) {
                                profession.push(stu.professionCode);
                            } else {
                                if (!profession.includes(stu.professionCode)) {
                                    this.$message({
                                        message: "请选择相同专业的学生",
                                        type: "warning"
                                    });
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            this.major = UTILS.isEmpty(profession) ? "" : profession[0];
            this.tutorGrade = UTILS.isEmpty(grades) ? null : grades[0];
            this.type = this.tutorType;
            this.queryTutorsParam.condition.type = this.tutorType;
            this.queryTutorsParam.condition.major = UTILS.isEmpty(profession)
                ? ""
                : profession[0];
            this.$refs.pointTutorsDialog.open();
        },

        pointTutor(data) {
            //学生指定导师
            this.showTitle = "导师指定名单";
            this.queryTutor = UTILS.clone(tutorForm);
            if (this.tutorType == 1) {
                this.queryTutor.schoolId = data.schoolId;
                this.queryTutor.tutorGrade = data.currentGrade;
            } else {
                this.queryTutor.faculty = data.faculty;
            }
            this.$refs.tutorDialog.open(() => {
                if (this.tutorType == 1) {
                    this.options.dialogSchool.length=0
                    //学堂下拉
                    this.querySchool.condition.grade = data.currentGrade;
                    BPIs.findSchool(this.querySchool).then(res => {
                        if (res.code == 200 && res.data.list.length > 0) {                            
                            for (const item of res.data.list) {
                                this.options.dialogSchool.push({
                                    label: item.name,
                                    value: item.id
                                });
                            }
                        }
                    });
                }

                if (this.queryTutor.schoolId || this.queryTutor.faculty) {
                    this.queryTutor.major = data.professionCode;
                } else {
                    this.queryTutor.major = "";
                }
                this.queryTutor.currentGrade = data.currentGrade;
                this.stuTutor = data;
                this.searchTutorList();
            });
        },
        release() {
            const item = { ids: [] };
            this.selectionList = this.$refs.mainTable.getCheckData();
            if (UTILS.isEmpty(this.selectionList)) {
                this.$message({
                    message: "请选择已经选择导师的学生进行发布",
                    type: "warning"
                });
                return;
            }
            for (const data of this.selectionList) {
                if (data.chooseStatus == 0) {
                    //未选择
                    this.$message({
                        message: "请选择已经选择导师的学生进行发布",
                        type: "warning"
                    });
                    return;
                }
                item.ids.push(data.id);
            }
            APIs.releaseTutor(item).then(res => {
                if (res.code == 200) {
                    this.searchList();
                }
            });
        },
        autoAllocation() {
            //自动分配异步
            this.selectionList = this.$refs.mainTable.getCheckData();
            if (UTILS.isEmpty(this.selectionList)) {
                this.$message({
                    message: "请选择未选择导师的学生进行自动分配",
                    type: "warning"
                });
                return;
            }
            this.$eduConfirm(
                this.$t(
                    "自动分配会将无导师的学生随机分配给对应导师，是否确定？"
                ),
                this.$t("系统提示")
            ).then(() => {
                for (const stu of this.selectionList) {
                    stu.type = this.tutorType;
                }
                let loading = this.$loading({
                    lock: true,
                    text: "Loading",
                    spinner: "el-icon-loading",
                    background: "rgba(255, 255, 255, .48)"
                });
                APIs.autoAllocation(this.selectionList).then(res => {
                    if (res.code == 200) {
                        this.delayRequestAuto(res.data.key, loading);
                    } else {
                        loading.close();
                    }
                });
            });
        },
        async delayRequestAuto(key, loading) {
            await new Promise(resolve => setTimeout(resolve, 5000));
            APIs.getStatuds(key)
                .then(res => {
                    if (res.code == 200 && res.data.success) {
                        this.$message({
                            message: res.data.msg,
                            type: "success"
                        });
                        this.searchList();
                        loading.close();
                    } else {
                        this.delayRequestAuto(key, loading);
                    }
                })
                .catch(ex => {
                    this.delayRequestAuto(key, loading);
                });
        },
        searchTutorList() {
            this.queryTutor.type = this.tutorType;
            this.$refs.tutorTable.search(this.queryTutor);
        },
        save(data) {
            const item = UTILS.clone(this.stuTutor);
            item.tutorId = data.id;
            item.teacherCode = data.teacherCode;
            item.teacherName = data.teacherName;
            item.type = this.tutorType;
            APIs.pointTutor(item).then(res => {
                if (res.code == 200) {
                    this.$refs.tutorDialog.close();
                }
            });
        },
        close() {
            this.$refs.tutorDialog.close();
            this.searchList();
        },
        resetDialog() {
            this.searchList();
        },
        resetTutor() {
            let tutorGrade = null;
            if (this.tutorType == 1) {
                tutorGrade = this.queryTutor.tutorGrade;
            }
            this.queryTutor = UTILS.clone(tutorForm);
            if (this.tutorType == 1) {
                this.queryTutor.tutorGrade = tutorGrade;
            }
            this.searchTutorList();
        },
        resetPointTutorsDialog() {
            this.searchList();
            this.$refs.queryTutors.clearValidate();
        },
        findSchool() {
            //学堂下拉
            BPIs.findSchool(this.querySchool).then(res => {
                if (res.code == 200 && res.data.list.length > 0) {
                    for (const item of res.data.list) {
                        this.options.school.push({
                            label: item.name,
                            value: item.id
                        });
                    }
                }
            });
        },
        submitFormTutors() {
            this.$refs.queryTutors.validate(valid => {
                if (valid) {
                    let item = {
                        tid: this.queryTutors.condition.condition,
                        tutorStudentList: UTILS.clone(this.selectionList)
                    };
                    APIs.pointTutors(item).then(res => {
                        if (res.code == 200) {
                            this.$refs.pointTutorsDialog.close();
                        }
                    });
                }
            });
        },
        delTutorList(val){
            let data = {}
            data.ids = [val.id]
            data.type = 1
            this.$delConfirm('确认要清除导师关系吗?').then(() => {
            APIs.delTutor(data).then(res =>{
                    if(res.code==200){
                        this.$message({
                            type: "success",
                            message: this.$t("清除成功")
                        });
                        this.searchList();
                    }
                }) 
            })
        },
        handleCurrentGradeChange(newVal){
            if (this.queryParams.schoolId) {
                this.queryParams.schoolId = null;
            }
            this.options.school = [];
            if (newVal) {
                this.querySchool.condition.grade = newVal;
                this.findSchool();
            }
        }
}
};
</script>
